﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverAmp.Shared;

namespace SilverAmp.Extensions
{
    [Export]
    public class SongEditExtensionViewModel:IPartImportsSatisfiedNotification,INotifyPropertyChanged 
    {
        [Import]
        public SongEditExtensionView View { get; set; }

        [Import]
        public MainModel Model { get; set; }

        [Import]
        public ExtensionsModel ExtensionsModel { get; set; }

        public ICommand UpdateSongInfoCommand { get; set; }

        public ITrackInfo Song
        {
            get { return Model.SelectedSong; }
        }
        public void OnImportsSatisfied()
        {
            View.DataContext = this;
        }

        public SongEditExtensionViewModel()
        {
            UpdateSongInfoCommand = new DelegateCommand<ISilverAmpExtension>(OnEditSongInformation);           
        }

        private void OnEditSongInformation(ISilverAmpExtension obj)
        {
            ISongInformationLookup lookup = obj as ISongInformationLookup;
            if (lookup != null)
            {
                lookup.Provider.SongDetailsUpdateComplete +=
                    new EventHandler<TrackEventArgs>(Provider_SongDetailsUpdateComplete);
                lookup.Provider.UpdateSongInformation(Song);
            }
        }

        void Provider_SongDetailsUpdateComplete(object sender, TrackEventArgs e)
        {
            Song.Artist = e.TrackInfo.Artist;            
        }

        public void Initialize()
        {
            if(PropertyChanged!=null)
                PropertyChanged(this,new PropertyChangedEventArgs(string.Empty));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
